﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.Data.SqlClient;
using TierApplication;


namespace OrderMgr.OrderInfo
{
    public partial class frmSaleInvoiceReturnFee : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _ReturnFeeID = "";
        public string _OrderID = "";
        public string _InvoiceID = "";
        public Utility.OrderType _OrderType;
        public Utility.OrderStatus _OrderStatus;

        T_OrderReturnFee mOrderReturnFee = new T_OrderReturnFee();

        #endregion

        public frmSaleInvoiceReturnFee()
        {
            InitializeComponent();
        }

        private void frmBuyToStockDetail_Load(object sender, EventArgs e)
        {
            initForm();
        }

        private void initForm()
        {
            //Utility.DataToLookupComboxEx(txtOrderType, C_DMB.GetListByType(C_DMB.DType.OrderType.ToString()), "CodeID", "CodeName");
            //Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtBuyUserID, (new P_User()).GetList(""), "UserID", "UserName");
            //Utility.DataToLookupComboxEx(txtAuditUserID, (new P_User()).GetList(""), "UserID", "UserName");
            //Utility.DataToLookupComboxEx(txtInvoiceUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtInvoiceUserID, (new P_User()).GetList(""), "UserID", "UserName");
            txtInvoiceID.Text = _InvoiceID;
            //txtOrderUserID.Enabled = false;
            switch (_OrderStatus)
            {
                case Utility.OrderStatus.S0Input:
                    btnSubmitAudit.Enabled = false;
                    break;
                case Utility.OrderStatus.S1Auditing:

                    break;
                case Utility.OrderStatus.All:
                    break;
                default:
                    break;
            }

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    btnAdd.Visible = false;
                    btnDelete.Visible = false;
                    btnModify.Visible = false;
                    break;
                case Utility.OperateMode.Insert:
                    break;
                case Utility.OperateMode.Update:
                    break;
                case Utility.OperateMode.Delete:
                    break;
                case Utility.OperateMode.Extend:
                    break;
                case Utility.OperateMode.Select:
                    break;
                default:
                    break;
            }

        }


        private void showInfo(string sID)
        {
            mOrderReturnFee.GetModel(sID);
            this.txtInvoiceID.Text = mOrderReturnFee.InvoiceID;
           
            //*****************Order Detail*****************//
            BindOrderDetail();

        }
        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }


        private void BindOrderDetail()
        {

            if (_ReturnFeeID == "") return;

            BindSaleShipByOrderID(txtInvoiceID.Text);
           
        }


        private void txtPOrderID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmSaleInvoiceNeed fDetail = new frmSaleInvoiceNeed();
            fDetail.gFuncType = Utility.FuncType.Select;
            fDetail._IsStock = "Y";
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtInvoiceID.Text = fDetail._OrderID;
                string sPOrderID = txtInvoiceID.Text;
                //生成商品明细

                BindSaleShipByOrderID(sPOrderID);
               
            }
        }

    


        private void BindSaleShipByOrderID(string sSaleOrderID)
        {
            DataTable dtOrderDetail = T_Order.GetSaleShipByOrderID(sSaleOrderID).Tables[0];
            gcSaleProduct.DataSource = dtOrderDetail;

            BindOrderReturnFeeByInvoiceID();

        }

        private void BindOrderReturnFeeByInvoiceID()
        {
            T_OrderReturnFee mOrderReturnFee = new T_OrderReturnFee();
            DataTable dtOrderReturnFee = mOrderReturnFee.GetSaleReturnFeeByInvoiceID(txtInvoiceID.Text).Tables[0];
            gcOrderReturnFee.DataSource = dtOrderReturnFee;

        }

        private void btnAudit_Click(object sender, EventArgs e)
        {

        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmSaleReturnFeeDetail fDetail = new frmSaleReturnFeeDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._InvoiceID = txtInvoiceID.Text;
            fDetail._OrderType = Utility.OrderType.SaleReturnFee;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindOrderReturnFeeByInvoiceID();
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvOrderReturnFee.RowCount < 1) return;
            DataRow dr = gvOrderReturnFee.GetDataRow(gvOrderReturnFee.FocusedRowHandle);
            string sReturnFeeID = dr["ReturnFeeID"].ToString();

            frmSaleReturnFeeDetail fDetail = new frmSaleReturnFeeDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Update;
            fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._ReturnFeeID = sReturnFeeID;
            fDetail._InvoiceID = txtInvoiceID.Text;
            fDetail._OrderType = Utility.OrderType.SaleReturnFee;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindOrderReturnFeeByInvoiceID();
            }
        }

        private void txtInvoiceID_EditValueChanged(object sender, EventArgs e)
        {
            string sInvoiceID = txtInvoiceID.Text;
            T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
            mOrderInvoice.GetModel(sInvoiceID);
            txtInvoiceUserID.EditValue = mOrderInvoice.OrderUserID;
            if (mOrderInvoice.OrderDate != null)
                txtInvoiceDate.DateTime = mOrderInvoice.OrderDate.Value;
            txtInvoiceMoney.Value = mOrderInvoice.InvoiceMoney.Value;

            string sSaleOrderID = mOrderInvoice.OrderID;
            T_Order mOrder = new T_Order();
            mOrder.GetModel(sSaleOrderID);
            txtSaleInvoiceMoney.Value = mOrder.PayFee.Value;
            txtOrderID.Text = sSaleOrderID;
            txtBuyDate.DateTime = mOrder.OrderDate.Value;

            _OrderID = sSaleOrderID;
            T_Customer mCustomer = new T_Customer();
            mCustomer.GetModel(mOrder.CustID);
            txtCustName.Text = mCustomer.CustName;
          
            BindSaleShipByOrderID(sSaleOrderID);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvOrderReturnFee.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此订单记录，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvOrderReturnFee.GetDataRow(gvOrderReturnFee.FocusedRowHandle);
            string sID = dr["ReturnFeeID"].ToString();

            T_OrderReturnFee mOrder = new T_OrderReturnFee();
            mOrder.Delete(sID);
            BindOrderReturnFeeByInvoiceID();
        }

    }
}